/**
 * 
 */
package com.sasis.webapp.task.ftp;

import java.io.InputStream;
import java.util.TreeSet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sasis.util.PropertiesUtil;

/**
 * @author Donitz
 * @Email zhengy@travelsky.com
 */
public class FtpbkoConfig implements java.io.Serializable{

	private static Log log = LogFactory.getLog(FtpbkoConfig.class);
	private FtpbkoConfig() {
		
	}
	/**
	 * local save path,the most right must be matched '\'
	 */
	public static String LOCAL_DOWNLOAD_PATH;
	/**
	 * downloaded date flag
	 */
	public static String TAG_DOWNLOADEDFILE_NAME;
	/**
	 * compare tagDate substring time begin index
	 */
	public static int SUB_FILETIME_BEGIN;
	/**
	 * compare tagDate substring time end index
	 */
	public static int SUB_FILETIME_END;
	/**
	 * compare Flag.data file tagDate substring time begin index
	 */
	public static int SUB_FLAGFILE_BIGIN;
	/**
	 * compare Flag.data file substring time end index
	 */
	public static int SUB_FLAGFILE_END;
	/**
	 * uncompressed file name substring begin index
	 */
	public static int SUB_GZIPFILE_BEGIN;
	/**
	 * uncompressed file name substring end index
	 */
	public static int SUB_GZIPFILE_END;
	/**
	 * max of bko file length
	 */
	public static int LENGTH_BKOFILE_MAX;
	/**
	 * max of flag file length
	 */
	public static int LENGTH_FLAGFILE_MAX;
	/**
	 * flag file prefix name
	 */
	public static String NAME_FLAGFILE_PREFIX;
	/**
	 * flag file postfix name
	 */
	public static String NAME_FLAGFILE_POSTFIX;
	/**
	 * bko file prefix name
	 */
	public static String NAME_BKOFILE_PREFIX;
	/**
	 * bko file postfix name
	 */
	public static String NAME_BKOFILE_POSTFIX;
	/**
	 * must be matched '\' on the most right
	 */
	public static String REMOTE_BACKUP_PATH;
//	/**
//	 * FTP file sorted list
//	 */
//	public static TreeSet treeSet ;
//	/**
//	 * FTP flag file list;
//	 */
//	public static TreeSet treeSetFlag ;
	/**
	 * FTP connection
	 */
	public static IFtpClient FTP_CLIENT ;
	/**
	 * compress & uncompress processor
	 */
	public static Compressor COMPRESSOR;
	
	/**
	 * initialize all field
	 *TODO maybe add some null check for all field
	 */
	public static boolean init(){
		String propName = "bko2.properties";
		InputStream in = FtpbkoConfig.class.getResourceAsStream(propName);
		if(in==null){
			log.error(" We can't find "+propName+" file in the directory which contains file FtpbkoConfig.class");
			return false;
		}
        PropertiesUtil prop = new PropertiesUtil(in);
        LOCAL_DOWNLOAD_PATH = prop.getValue("LOCAL_DOWNLOAD_PATH");
        TAG_DOWNLOADEDFILE_NAME = prop.getValue("TAG_DOWNLOADEDFILE_NAME");
        SUB_FILETIME_BEGIN = Integer.parseInt(prop.getValue("SUB_FILETIME_BEGIN"));
        SUB_FILETIME_END = Integer.parseInt(prop.getValue("SUB_FILETIME_END"));
        SUB_FLAGFILE_BIGIN = Integer.parseInt(prop.getValue("SUB_FLAGFILE_BIGIN"));
        SUB_FLAGFILE_END = Integer.parseInt(prop.getValue("SUB_FLAGFILE_END"));
        SUB_GZIPFILE_BEGIN = Integer.parseInt(prop.getValue("SUB_GZIPFILE_BEGIN"));        
        SUB_GZIPFILE_END = Integer.parseInt(prop.getValue("SUB_GZIPFILE_END"));
        LENGTH_BKOFILE_MAX = Integer.parseInt(prop.getValue("LENGTH_BKOFILE_MAX"));
        LENGTH_FLAGFILE_MAX = Integer.parseInt(prop.getValue("LENGTH_FLAGFILE_MAX"));
        NAME_FLAGFILE_PREFIX = prop.getValue("NAME_FLAGFILE_PREFIX");
        NAME_FLAGFILE_POSTFIX = prop.getValue("NAME_FLAGFILE_POSTFIX");
        NAME_BKOFILE_PREFIX = prop.getValue("NAME_BKOFILE_PREFIX");
        NAME_BKOFILE_POSTFIX = prop.getValue("NAME_BKOFILE_POSTFIX");
        REMOTE_BACKUP_PATH = prop.getValue("REMOTE_BACKUP_PATH");
      
		COMPRESSOR = new GzipTools();
		   FtpClientBKOImpl ftpClient = new FtpClientBKOImpl();
		   ftpClient.setHostAdress(prop.getValue("hostAdress"));
		   ftpClient.setUser(prop.getValue("ftpLoginUser"));
		   ftpClient.setPassword(prop.getValue("ftpLoginPasswd"));
		   ftpClient.setPort(Integer.parseInt(prop.getValue("port")));
		   ftpClient.setTimeout(Integer.parseInt(prop.getValue("timeout")));
		   ftpClient.setSocksAddress(prop.getValue("socksAddress"));
		   ftpClient.setSocksPort(Integer.parseInt(prop.getValue("socksPort")));	
		FTP_CLIENT = ftpClient;
		
//		treeSet = new TreeSet();
//		treeSetFlag = new TreeSet();
		return true;
	}
	
	

}
